package com.qdhh.enrollment.controller.admin;

import com.qdhh.enrollment.common.Result;
import com.qdhh.enrollment.model.AdminUser;
import com.qdhh.enrollment.service.AdminService;
import com.qdhh.enrollment.service.dto.AdminLoginResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 管理员认证控制器
 */
@Tag(name = "管理员认证", description = "管理员登录、登出、获取当前用户信息接口")
@RestController
@RequestMapping("/api/admin/auth")
public class AdminAuthController {

    private final AdminService adminService;

    public AdminAuthController(AdminService adminService) {
        this.adminService = adminService;
    }

    @Operation(summary = "管理员登录", description = "管理员账号密码登录")
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody LoginRequest request) {
        AdminLoginResult loginResult = adminService.login(request.getUsername(), request.getPassword());
        
        Map<String, Object> data = new HashMap<>();
        data.put("token", loginResult.getToken());
        data.put("admin", loginResult.getAdmin());
        
        return Result.success("登录成功", data);
    }

    @Operation(summary = "获取当前管理员信息", description = "获取当前登录的管理员信息")
    @GetMapping("/me")
    public Result<AdminUser> getCurrentAdmin() {
        AdminUser admin = adminService.getCurrentAdmin();
        return Result.success(admin);
    }

    @Operation(summary = "登出", description = "管理员登出")
    @PostMapping("/logout")
    public Result<Void> logout() {
        adminService.logout();
        return Result.success("登出成功", null);
    }

    public static class LoginRequest {
        private String username;
        private String password;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }
}

